package com.team.cloudsharing.controller.file;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.team.cloudsharing.dto.File;
import com.team.cloudsharing.dto.Message;
import com.team.cloudsharing.dto.PathDTO;
import com.team.cloudsharing.dto.TextFile;
import com.team.cloudsharing.service.file.FileService;

@RestController
@RequestMapping("/file")
public class FileControllerImpl implements FileController{
	
	final static Logger logger = Logger.getLogger(FileControllerImpl.class);

	@Autowired
	FileService fileService;
	
	@Override
	@RequestMapping(value = "/saveFileUploaded", method = RequestMethod.POST)
	public ResponseEntity<Message> saveFileUploaded(@RequestBody File file) {
		logger.info("POST /file/saveFileUploaded - file: " + file.toString());
		String statusSaveFileUploaded = fileService.saveFileUploaded(file);
		return new ResponseEntity<Message>(new Message(statusSaveFileUploaded), HttpStatus.OK);
	}
	
	@Override
	@RequestMapping(value = "/downloadFile/{fileUrl}", method = RequestMethod.GET)
	public ResponseEntity<File> downloadFile(@PathVariable("fileUrl") String fileUrl) {
		logger.info("POST /file/downloadFile - fileUrl: " + fileUrl);
		File fileDownload = fileService.downloadFile(fileUrl);
		return new ResponseEntity<File>(fileDownload, HttpStatus.OK);
	}

	@Override
	@RequestMapping(value = "/moveFileToTrash", method = RequestMethod.POST)
	public ResponseEntity<Message> moveFileToTrash(@RequestBody File file) {
		logger.info("POST /file/moveFileToTrash - file: " + file.toString());
		String statusSaveFileUploaded = fileService.moveFileToTrash(file);
		return new ResponseEntity<Message>(new Message(statusSaveFileUploaded), HttpStatus.OK);
	}

	@Override
	@RequestMapping(value = "/restoreFileFromTrash", method = RequestMethod.POST)
	public ResponseEntity<Message> restoreFileFromTrash(@RequestBody File file) {
		logger.info("POST /file/moveFileToTrash - file: " + file.toString());
		String statusSaveFileUploaded = fileService.restoreFileFromTrash(file);
		return new ResponseEntity<Message>(new Message(statusSaveFileUploaded), HttpStatus.OK);
	}

	@Override
	@RequestMapping(value = "/deleteFile", method = RequestMethod.POST)
	public ResponseEntity<Message> deleteFile(@RequestBody File file) {
		logger.info("POST /file/deleteFile - file: " + file.toString());
		String statusDeleteFile = fileService.deleteFile(file);
		return new ResponseEntity<Message>(new Message(statusDeleteFile), HttpStatus.OK);
	}

	@Override
	@RequestMapping(value = "/renameFile", method = RequestMethod.POST)
	public ResponseEntity<Message> renameFile(@RequestBody PathDTO filePath) {
		logger.info("POST /file/renameFile - renameFile: " + filePath.toString());
		String statusRenameFile = fileService.renameFile(filePath);
		return new ResponseEntity<Message>(new Message(statusRenameFile), HttpStatus.OK);
	}
	
//	@Override
//	@RequestMapping(value = "/moveFile", method = RequestMethod.POST)
//	public ResponseEntity<Message> moveFile(@RequestBody PathDTO filePath) {
//		logger.info("POST /file/moveFile - moveFile: " + filePath.toString());
//		String statusMoveFile = fileService.moveFile(filePath);
//		return new ResponseEntity<Message>(new Message(statusMoveFile), HttpStatus.OK);
//	}
	
//	@Override
//	@RequestMapping(value = "/saveFile", method = RequestMethod.POST)
//	public ResponseEntity<Message> saveFile(@RequestBody File file) {
//		logger.info("POST /file/saveFile - saveFile: " + file.getFileName());
//		String statusSaveFile = fileService.saveFile(file.getFilePath(), file.getFileName(), file.getFileCapacity(), file.getFilePermission(), file.getUserEmail());
//		return new ResponseEntity<Message>(new Message(statusSaveFile), HttpStatus.OK);
//	}

	@Override
	@RequestMapping(value="/getFileUrl", method=RequestMethod.GET)
	public ResponseEntity<String> getFileUrlByFileId(@RequestParam("fileId") String fileId) {
		logger.info("POST /file/getFileUrl - fileId: " +fileId);
		String fileUrl = fileService.getFileUrlByFileId(fileId);
		return new ResponseEntity<String>(fileUrl, HttpStatus.OK);
	}
	
	@Override
	@RequestMapping(value = "/moveFileToAnotherFolder", method = RequestMethod.POST)
	public ResponseEntity<Message> moveFileToAnotherFolder(@RequestBody PathDTO pathDTO) {
		logger.info("POST /file/moveFileToAnotherFolder - moveFile: " + pathDTO.toString());
		String statusMoveFile = fileService.moveFileToAnotherFolder(pathDTO);
		return new ResponseEntity<Message>(new Message(statusMoveFile), HttpStatus.OK);
	}
	
	@Override
	@RequestMapping(value = "/copyFileToAnotherFolder", method = RequestMethod.POST)
	public ResponseEntity<Message> copyFileToAnotherFolder(@RequestBody PathDTO pathDTO) {
		logger.info("POST /file/copyFileToAnotherFolder - copyFile: " + pathDTO.toString());
		String statusCopyFile = fileService.copyFileToAnotherFolder(pathDTO);
		return new ResponseEntity<Message>(new Message(statusCopyFile), HttpStatus.OK);
	}

	@Override
	@RequestMapping(value = "/createTextFile", method = RequestMethod.POST)
	public ResponseEntity<Message> createTextFile(@RequestBody TextFile textFile) {
		String statusCreate = fileService.createTextFile(textFile);
		return new ResponseEntity<Message>(new Message(statusCreate), HttpStatus.OK);
	}
}
